home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Help / Regular Expressions < prev    next >
Encoding:
Text File  |  2000-05-23  |  8.3 KB  |  184 lines

  1.                    Regular Expressions
  2.            
  3. What follows is the man page for the regular expression package used
  4. in Alpha 7.x.  For Alphatk and Alpha 8 the default Tcl regular
  5. expression package is used, and you should look at Tcl's documentation
  6. for the advanced regular expression support provided.
  7.  
  8. Caveat: In Alpha 8, currently 'search' still uses the old Alpha 7
  9. regular expressions, whereas other commands use the new Tcl 8.3
  10. versions.
  11.  
  12.  
  13.      NAME
  14.           regcomp, regexec, regsub, regerror - regular expression
  15.           handler
  16.  
  17.      SYNOPSIS
  18.           #include <regexp.h>
  19.  
  20.           regexp *regcomp(exp)
  21.           char *exp;
  22.  
  23.           int regexec(prog, string)
  24.           regexp *prog;
  25.           char *string;
  26.  
  27.           regsub(prog, source, dest)
  28.           regexp *prog;
  29.           char *source;
  30.           char *dest;
  31.  
  32.           regerror(msg)
  33.           char *msg;
  34.  
  35.      DESCRIPTION
  36.           These functions implement egrep(1)-style regular expressions
  37.           and supporting facilities.
  38.  
  39.           Regcomp compiles a regular expression into a structure of
  40.           type regexp, and returns a pointer to it.  The space has
  41.           been allocated using malloc(3) and may be released by free.
  42.  
  43.           Regexec matches a NUL-terminated string against the compiled
  44.           regular expression in prog.  It returns 1 for success and 0
  45.           for failure, and adjusts the contents of prog's startp and
  46.           endp (see below) accordingly.
  47.  
  48.           The members of a regexp structure include at least the
  49.           following (not necessarily in order):
  50.  
  51.                char *startp[NSUBEXP];
  52.                char *endp[NSUBEXP];
  53.  
  54.           where NSUBEXP is defined (as 10) in the header file.  Once a
  55.           successful regexec has been done using the regexp, each
  56.           startp-endp pair describes one substring within the string,
  57.           with the startp pointing to the first character of the
  58.           substring and the endp pointing to the first character
  59.           following the substring.  The 0th substring is the substring
  60.           of string that matched the whole regular expression.  The
  61.           others are those substrings that matched parenthesized
  62.           expressions within the regular expression, with
  63.           parenthesized expressions numbered in left-to-right order of
  64.           their opening parentheses.
  65.  
  66.           Regsub copies source to dest, making substitutions according
  67.           to the most recent regexec performed using prog.  Each
  68.           instance of `&' in source is replaced by the substring
  69.           indicated by startp[0] and endp[0].  Each instance of `\n',
  70.           where n is a digit, is replaced by the substring indicated
  71.           by startp[n] and endp[n].  To get a literal `&' or `\n' into
  72.           dest, prefix it with `\'; to get a literal `\' preceding `&'
  73.           or `\n', prefix it with another `\'.
  74.  
  75.           Regerror is called whenever an error is detected in regcomp,
  76.           regexec, or regsub.  The default regerror writes the string
  77.           msg, with a suitable indicator of origin, on the standard
  78.           error output and invokes exit(2).  Regerror can be replaced
  79.           by the user if other actions are desirable.
  80.  
  81.      REGULAR EXPRESSION SYNTAX
  82.           A regular expression is zero or more branches, separated by
  83.           `|'.  It matches anything that matches one of the branches.
  84.  
  85.           A branch is zero or more pieces, concatenated.  It matches a
  86.           match for the first, followed by a match for the second,
  87.           etc.
  88.  
  89.           A piece is an atom possibly followed by `*', `+', or `?'.
  90.           An atom followed by `*' matches a sequence of 0 or more
  91.           matches of the atom.  An atom followed by `+' matches a
  92.           sequence of 1 or more matches of the atom.  An atom followed
  93.           by `?' matches a match of the atom, or the null string.
  94.  
  95.           An atom is a regular expression in parentheses (matching a
  96.           match for the regular expression), a range (see below), `.'
  97.           (matching any single character), `^' (matching the null
  98.           string at the beginning of the input string), `$' (matching
  99.           the null string at the end of the input string), a `\'
  100.           followed by a single character (matching that character), or
  101.           a single character with no other significance (matching that
  102.           character).
  103.  
  104.           A range is a sequence of characters enclosed in `[]'.  It
  105.           normally matches any single character from the sequence.  If
  106.           the sequence begins with `^', it matches any single
  107.           character not from the rest of the sequence.  If two
  108.           characters in the sequence are separated by `-', this is
  109.           shorthand for the full list of ASCII characters between them
  110.           (e.g. `[0-9]' matches any decimal digit).  To include a
  111.           literal `]' in the sequence, make it the first character
  112.           (following a possible `^').  To include a literal `-', make
  113.           it the first or last character.
  114.  
  115.      AMBIGUITY
  116.           If a regular expression could match two different parts of
  117.           the input string, it will match the one which begins
  118.           earliest.  If both begin in the same place    but match
  119.           different lengths, or match the same length in different
  120.           ways, life gets messier, as follows.
  121.  
  122.           In general, the possibilities in a list of branches are
  123.           considered in left-to-right order, the possibilities for
  124.           `*', `+', and `?' are considered longest-first, nested
  125.           constructs are considered from the outermost in, and
  126.           concatenated constructs are considered leftmost-first.  The
  127.           match that will be chosen is the one that uses the earliest
  128.           possibility in the first choice that has to be made.  If
  129.           there is more than one choice, the next will be made in the
  130.           same manner (earliest possibility) subject to the decision
  131.           on the first choice.  And so forth.
  132.  
  133.           For example, `(ab|a)b*c' could match `abc' in one of two
  134.           ways.  The first choice is between `ab' and `a'; since `ab'
  135.           is earlier, and does lead to a successful overall match, it
  136.           is chosen.  Since the `b' is already spoken for, the `b*'
  137.           must match its last possibility-the empty string-since it
  138.           must respect the earlier choice.
  139.  
  140.           In the particular case where no `|'s are present and there
  141.           is only one `*', `+', or `?', the net effect is that the
  142.           longest possible match will be chosen.  So `ab*', presented
  143.           with `xabbbby', will match `abbbb'.  Note that if `ab*' is
  144.           tried against `xabyabbbz', it will match `ab' just after
  145.           `x', due to the begins-earliest rule.  (In effect, the
  146.           decision on where to start the match is the first choice to
  147.           be made, hence subsequent choices must respect it even if
  148.           this leads them to less-preferred alternatives.)
  149.  
  150.       In addition, \w matches an alphanumeric character (including "_") 
  151.       and \W a nonalphanumeric. Word boundaries may be matched by \b, 
  152.       and non-boundaries by \B. A whi- tespace character is matched by 
  153.       \s, non-whitespace by \S. A numeric character is matched by \d, 
  154.       non-numeric by \D. You may use \w, \s and \d within character 
  155.       classes.
  156.           
  157.       The class of character recognized by \w (and hence not recognized 
  158.       by \W), can be augmented via the 'addAlphaChars' command.
  159.  
  160.      DIAGNOSTICS
  161.           Regcomp returns NULL for a failure (regerror permitting),
  162.           where failures are syntax errors, exceeding implementation
  163.           limits, or applying `+' or `*' to a possibly-null operand.
  164.  
  165.      HISTORY
  166.           Both code and manual page were written at U of T.  They are
  167.           intended to be compatible with the Bell V8 regexp(3), but
  168.           are not derived from Bell code.
  169.  
  170.      BUGS
  171.           Empty branches and empty regular expressions are not
  172.           portable to V8.
  173.  
  174.           The restriction against applying `*' or `+' to a possibly-
  175.           null operand is an artifact of the simplistic
  176.           implementation.
  177.  
  178.           Does not support egrep's newline-separated branches; neither
  179.           does the V8 regexp(3), though.
  180.  
  181.           Due to emphasis on compactness and simplicity, it's not
  182.           strikingly fast.  It does give special attention to handling
  183.           simple cases quickly.
  184.